package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"vehicle-hub/cache"
	"vehicle-hub/models"
)

func Add(c *gin.Context) {
	car_code := c.PostForm("car_code")
	car_name := c.PostForm("car_name")
	car_price, _ := strconv.Atoi(c.PostForm("car_price"))

	data := models.Car{
		CarCode:  car_code,
		CarName:  car_name,
		CarPrice: car_price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_name := c.PostForm("order_name")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	cid, _ := strconv.Atoi(c.PostForm("cid"))

	data := models.Order{
		OrderNo:    order_no,
		OrderName:  order_name,
		OrderPrice: order_price,
		Cid:        cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))

	Offset := (page - 1) * size

	var orderList []models.OrderList

	/*
			SELECT
		  cars.car_code AS CarCode,
		  cars.car_name AS CarName,
		  cars.car_price AS CarPrice,
		  orders.order_no AS OrderNo,
		  orders.order_name AS OrderName,
		  orders.order_price AS OrderPrice,
		  orders.cid AS Cid
		FROM
		  `orders`
		  LEFT JOIN cars on orders.cid=cars.id
		  LIMIT 3
	*/

	redis_key := "order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("orders").
			Joins("LEFT JOIN cars on orders.cid=cars.id").
			Select(
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_price AS CarPrice",
				"orders.order_no AS OrderNo",
				"orders.order_name AS OrderName",
				"orders.order_price AS OrderPrice",
				"orders.cid AS Cid").
			Offset(Offset).
			Limit(size).
			Debug().
			Find(&orderList)

		jsonData, _ := json.Marshal(orderList)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}

	var total int64
	models.DB.Model(&models.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  orderList,
		"total": total,
	})
	return

}

func Detail(c *gin.Context) {
	id := c.Query("id")

	var orderList models.OrderList

	/*
		SELECT
				  cars.car_code AS CarCode,
				  cars.car_name AS CarName,
				  cars.car_price AS CarPrice,
				  orders.order_no AS OrderNo,
				  orders.order_name AS OrderName,
				  orders.order_price AS OrderPrice,
				  orders.cid AS Cid
				FROM
				  `orders`
				  LEFT JOIN cars on orders.cid=cars.id
				  WHERE orders.id=3
	*/

	redis_key := "order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("orders").
			Joins("LEFT JOIN cars on orders.cid=cars.id").
			Select(
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_price AS CarPrice",
				"orders.order_no AS OrderNo",
				"orders.order_name AS OrderName",
				"orders.order_price AS OrderPrice",
				"orders.cid AS Cid").
			Where("orders.id=?", id).
			Debug().
			Find(&orderList)

		jsonData, _ := json.Marshal(orderList)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": orderList,
	})
	return
}
